function z = resid3vNoL(X)
global alpha betta depr sigmaC phi govexp taumax N T e_real
global kstart kistart popweight mistart transfer deductible restrict
global Delta1 Delta2 ALPHA lamda tauK0

if isreal(X)==0
  z(1) = 1e100;
  return
end

n = size(X,1);
z = zeros(n,1);

Delta1 = X(3*T+1);
Delta2=-Delta1*phi(1)/phi(2); %ONLY IF N=2
lamda = X(3*T+2:3*T+N);
transfer=0;

if restrict == 0
  Delta1=0;
  Delta2 = Delta1;
  transfer=X(3*T+1);
end
% elseif restrict == 1
%   deductible = X(3*T+2);
%   Delta2 = - Delta1;
% else
%   Delta2 = X(3*T+2); 
% end

options = optimset('MaxFunEvals',1000000,'MaxIter',1000,'TolFun',1e-8,'TolX',1e-8,'Disp','off');
mustst = fzero('findststNoL',1,options);%,0,0)
rstst = 1/betta-1+depr; %r from Euler at the steady state
kstst=e_real/(rstst/alpha)^(1/(1-alpha)); % express k from r=F_k
wstst = (1-alpha)*kstst^alpha*e_real^(-alpha); %w=F_e
lstst=e_real;
cstst = 1/(popweight(1)+sum(popweight(2:N).*lamda))*(kstst^alpha*e_real^(1-alpha) - depr*kstst - govexp); %c from resource constraint
mustst=(cstst^(-sigmaC)+sum(ALPHA.*lamda.*(lamda*cstst).^(-sigmaC)))/(popweight(1)+sum(popweight(2:N).*lamda));

if size(X,1)>1
    X=X';
end
klast = [kstart X(1:T-1)]; %k_{t-1}
k = X(1:T);
mu= X(T+1:2*T);
gamma = X(2*T+1:3*T); %gamma_t

e=ones(1,T)*e_real;
l=e;

r = alpha*klast.^(alpha-1).*e.^(1-alpha); %r=F_k
w = (1-alpha)*klast.^(alpha).*e.^(-alpha); %w=F_e
Fke = alpha*(1-alpha)*klast.^(alpha-1).*e.^(-alpha);
Fkk = alpha*(alpha-1)*klast.^(alpha-2).*e.^(1-alpha);
c = 1/(popweight(1)+sum(popweight(2:N).*lamda))*(klast.^alpha.*e.^(1-alpha)+ (1-depr)*klast - k - govexp); %resource constraint

cnext = [c(2:end) cstst];
rnext = [r(2:end) rstst];
Fkknext = [Fkk(2:end) alpha*(alpha-1)*k(end).^(alpha-2).*e_real.^(1-alpha)];

%check bound on tauk
constrtest = c.^(-sigmaC)./cnext.^(-sigmaC) - betta*(1+ (rnext-depr)*(1-taumax));
ind = max(sign(constrtest),0); %ind = 1 if constraint doesn't bind

%period 0
c0 = c(1);
w0 = w(1);
r0 = r(1);
Fke0 = Fke(1);

%FOC for consumption at time 0
gammanew(1) =(mu(1)*(popweight(1)+sum(popweight(2:N).*lamda))-c0^(-sigmaC)-sum(ALPHA.*lamda.*(lamda*c0).^(-sigmaC))...
    -sigmaC*c0^(-sigmaC-1)*(Delta1*kistart(1)+sum(Delta2.*kistart(2:N)))*(1+(r0-depr)*(1-tauK0)))/(-sigmaC*c0^(-sigmaC-1));
    %Delta1*(mistart(1)-transfer+deductible)+Delta2*(mistart(2)+transfer+deductible));

%periods t>0
lt = l(2:T);
ct = c(2:T);
wt = w(2:T);
rt = r(2:T);
Fket = Fke(2:T);

%FOC for consumption at t>0
gammanew(2:T) = (mu(2:T)*(popweight(1)+sum(popweight(2:N).*lamda))-ct.^(-sigmaC)-((ct'.^(-sigmaC)*(ALPHA.*lamda.^(1-sigmaC)))*ones(N-1,1))'...
    -gamma(1:T-1)*sigmaC.*ct.^(-sigmaC-1).*(1+(rt-depr)*(1-taumax)))./(-sigmaC*ct.^(-sigmaC-1));

munext = [mu(2:end) mustst];

z(1:T) = gammanew - gamma;

if restrict==0
    %FOC for consumption at time 0
z(1) =(mu(1)*(popweight(1)+sum(popweight(2:N).*lamda))-c0^(-sigmaC)-sum(ALPHA.*lamda.*(lamda*c0).^(-sigmaC))...
    -sigmaC*c0^(-sigmaC-1)*(Delta1*kistart(1)+sum(Delta2.*kistart(2:N)))*(1+(r0-depr)*(1-tauK0)))/(-sigmaC*c0^(-sigmaC-1));
    %Delta1*(mistart(1)-transfer+deductible)+Delta2*(mistart(2)+transfer+deductible));
%FOC for consumption at t>0
z(2:T) = (mu(2:T)*(popweight(1)+sum(popweight(2:N).*lamda))-ct.^(-sigmaC)-((ct'.^(-sigmaC)*(ALPHA.*lamda.^(1-sigmaC)))*ones(N-1,1))'...
    -gamma(1:T-1)*sigmaC.*ct.^(-sigmaC-1).*(1+(rt-depr)*(1-taumax)))./(-sigmaC*ct.^(-sigmaC-1));
gammanew = gamma;
end

z(T+1:2*T) = ind.*gamma +(1-ind).*constrtest; %either gamma is zero and the constraint is slack (ind=1), or the constraint holds as equalitity
z(2*T+1:3*T) = mu - betta*munext.*(1-depr+rnext) + betta*gammanew.*cnext.^(-sigmaC).*Fkknext*(1-taumax); %FOC for capital

%present value budget constraint for group 1 (at steady state from period T
%onwards) to express tauh
tauh= 1-(sum(betta.^[0:T-1].*(c.^(-sigmaC).*c)) + betta^T/(1-betta)*cstst^(-sigmaC)*cstst-c(1)^(-sigmaC)*(kistart(1)*(1+(r(1)-depr)*(1-tauK0))-transfer))/(sum(betta.^[0:T-1].*c.^(-sigmaC)*phi(1).*w.*l)+betta^T/(1-betta)*cstst^(-sigmaC)*phi(1)*wstst*e_real);
%present value budget constraint for group 2 (at steady state from period T onwards)
for jj=2:N
z(3*T+jj-1) = sum(betta.^[0:T-1].*(c.^(-sigmaC).*(lamda(jj-1)*c-(1-tauh)*phi(jj)*w.*l)))+betta^T/(1-betta)*(cstst^(-sigmaC)*(lamda(jj-1)*cstst - (1-tauh)*phi(jj)*wstst*e_real))-c(1)^(-sigmaC)*(kistart(jj)*(1+(r(1)-depr)*(1-tauK0))+transfer);
end

%FOC for lamda
for jj=1:N-1
z(3*T+N-1+jj) = sum(betta.^[0:T-1].*(ALPHA(jj)*(lamda(jj)*c).^(-sigmaC).*c+Delta2(jj)*c.^(-sigmaC).*c-mu/2.*c))+betta^T/(1-betta)*(ALPHA(jj)*(lamda(jj)*cstst)^(-sigmaC)*cstst+Delta2(jj)*cstst^(-sigmaC)*cstst-mustst/2*cstst);
end
